<?php

require_once 'mysqlconnect.php';

function getAllType() {//获得所有类型
	global $dbc;
	$types = array ();
	
	$query = "SELECT * FROM goods_type";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	while ( $row ) {
		$oneType = array ();
		$oneType [0] = $row [0];
		$oneType [1] = $row [1];
		$oneType [2] = $row [2];
		
		$types [] = $oneType;
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	}
	return $types;
}

function newType($fatherid, $typeName) {//添加商品类型
	global $dbc;
	$fatherid = escape_data ( $fatherid );
	if($fatherid == ""){
		$fatherid = -1;
	}
	$typeName = escape_data ( $typeName );
	
	$query = "INSERT INTO goods_type (fathertype, gtypename) VALUES ('" . $fatherid . "','" . $typeName . "')";
	$result = mysqli_query ( $dbc, $query );
	if($result){
		return TRUE;
	}else{
		return FALSE;
	}
}

function mAddGoods($name, $price, $left_count, $illustrate, $typeid) {//添加商品
	global $dbc;
	
	$name = escape_data ( $name );
	$price = escape_data ( $price );
	$left_count = escape_data ( $left_count );
	$illustrate = escape_data ( $illustrate );
	$typeid = escape_data ( $typeid );
	
	$query = "INSERT INTO goods_baseinfo (gname, gprice, gleftcount) VALUES ('" . $name . "','" . $price . "','" . $left_count . "')";
	$result1 = mysqli_query ( $dbc, $query );
	$a = mysqli_insert_id ( $dbc );
	
	$time = date ( "Y-m-d H:i:s" );
	$query = "INSERT INTO goods_moreinfo (gid, gillustrate, gtime, gtypeid) VALUES ('" . $a . "','" . $illustrate . "','" . $time . "','" . $typeid . "')";
	$result2 = mysqli_query ( $dbc, $query );
	
	if ($result1 && $result2) {
		return TRUE;
	} else {
		return FALSE;
	}
}

function upLoadImage($imagePaht, $goodsid, $imageType) {//添加一系列照片
	global $dbc;
	$ok = TRUE;
	
	$goodsid = escape_data ( $goodsid );
	$imageType = escape_data ( $imageType );
	$imageCount = count ( $imagePaht );
	for($i = 0; $i < $imageCount; $i ++) {
		$imagePaht [$i] = escape_data ( $imagePaht [$i] );
		$query = "INSERT INTO goods_image (imagepath, goodsid, imagetype) VALUES ('" . $imagePaht[$i] . "','" . $goodsid . "','" . $imageType . "')";
		$result = mysqli_query ( $dbc, $query );
		if(!$result){
			$ok = $ok & FALSE;
			break;
		}
	}
	return $ok;
}

function findGoods($typeid, $pricemin, $pricemax) {//搜索商品
	global $dbc;
	$gids = array();
	
	$typeid = escape_data($typeid);
	$pricemin = escape_data($pricemin);
	$pricemax = escape_data($pricemax);
	
	$query = "SELECT gid FROM goods_moreinfo WHERE gtypeid='".$typeid."'";
	$result = mysqli_query ( $dbc, $query );
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	while ( $row ) {
		$oneid = $row[0];
		$query1 = "SELECT gprice FROM goods_baseinfo WHERE gid='".$oneid."'";
		$result1 = mysqli_query ( $dbc, $query1 );
		$row1 = mysqli_fetch_array ( $result1, MYSQL_NUM );
		if($row1){
			if($row1[0] > $pricemin && $row1[0] < $pricemax){
				$gids[] = $oneid;
			}
		}
		$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	}
	return $gids;
}

function changeBaseinfo($goodsid, $name, $addcount, $addprice, $illustrate){
	global $dbc;
	$goodsid = escape_data($goodsid);
	$addcount = escape_data($addcount);
	$name = escape_data($name);
	$addprice = escape_data($addprice);
	$illustrate = escape_data($illustrate);
	
	$query = "UPDATE goods_baseinfo SET gleftcount = gleftcount + '".$addcount."',gname='".$name."',gprice=gprice+'".$addprice."' WHERE gid='".$goodsid."'";
	$result1 = mysqli_query ( $dbc, $query );
	$query = "UPDATE goods_moreinfo SET gillustrate = '".$illustrate."' WHERE gid='".$goodsid."'";
	$result2 = mysqli_query ( $dbc, $query );
	if($result1 && $result2){
		return TRUE;
	}else{
		return FALSE;
	}
}

function getGoodsInfo($goodsID){
	global $dbc;
	$goodInfo = array();
	$goodsID = escape_data($goodsID);
	
	$query = "SELECT * FROM goods_baseinfo WHERE gid='".$goodsID."'";
	$result = mysqli_query($dbc, $query);
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	if($row){
		$goodInfo[] = $row[1];
		$goodInfo[] = $row[2];
		$goodInfo[] = $row[3];
		$goodInfo[] = $row[4];
		$goodInfo[] = $row[6];
	}
	$query = "SELECT * FROM goods_moreinfo WHERE gid='".$goodsID."'";
	$result = mysqli_query($dbc, $query);
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	if($row){
		$goodInfo[] = $row[1];
		$goodInfo[] = $row[2];
		$goodtypeid = $row[3];
	}
	$query = "SELECT * FROM goods_type WHERE gtypeid='".$goodtypeid."'";
	$result = mysqli_query($dbc, $query);
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	if($row){
		$goodInfo[] = $row[2];
	}
    $query = "SELECT * FROM goods_image WHERE goodsid='".$goodsID."' and imagetype='0'";
	$result = mysqli_query($dbc, $query);
	$row = mysqli_fetch_array ( $result, MYSQL_NUM );
	if($row){
		$goodInfo[] = $row[1];
	}
	
	return $goodInfo;
}

?>